#region license

// Copyright (c) 2008, Rinat Abdullin (http://rabdullin.com)
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//     * Neither the name of the Rinat Abdullin nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#endregion

using System;
using System.Collections.Generic;
using System.Reflection;
using Boo.Lang.Compiler.Ast;
using Boo.Lang.Compiler.Steps;
using Boo.Lang.Compiler.Util;
using Module=Boo.Lang.Compiler.Ast.Module;

namespace Photon.Dsl
{
	internal sealed class BaseClassCompilerStep : AbstractCompilerStep
	{
		private readonly IProvider<Module, IEnumerable<BaseClassInfo>> _provider;

		public BaseClassCompilerStep(IProvider<Module, IEnumerable<BaseClassInfo>> provider)
		{
			_provider = provider;
		}

		public override void Run()
		{
			foreach (var module in CompileUnit.Modules)
			{
				var imports = new HashSet<string>();

				foreach (var info in _provider.Get(module))
				{
					var member = CreateClass(module, info);
					module.Members.Add(member);

					imports.AddRange(info.Imports);
				}

				module.Globals = new Block();

				foreach (var s in imports)
				{
					module.Imports.Add(new Import(module.LexicalInfo, s));
				}
			}
		}

		private ClassDefinition CreateClass(Module module, BaseClassInfo info)
		{
			var baseClass = info.BaseClass;

			info.References
				.Prepend(baseClass.Assembly)
				.ForEach(AddAssembly);

			var definition = new ClassDefinition
			{
				Name = info.GetClassName(module)
			};


			if (baseClass.IsGenericTypeDefinition)
			{
				throw new NotImplementedException("IsGenericTypeDefinition");
			}
			if (baseClass.IsGenericType)
			{
				throw new NotImplementedException("IsGenericType");
			}
			definition.BaseTypes.Add(new SimpleTypeReference(baseClass.FullName));

			GenerateConstructors(info, definition);
			CreateMethod(info, module, definition);

			return definition;
		}

		private void AddAssembly(Assembly assembly)
		{
			if (Context.References.Contains(assembly) == false)
				Context.References.Add(assembly);
		}

		private static void CreateMethod(BaseClassInfo info, Module module, ClassDefinition definition)
		{
			var method = new Method(info.MethodName);

			foreach (ParameterDeclaration parameter in info.MethodParameters)
			{
				method.Parameters.Add(parameter);
			}

			method.Body = module.Globals;
			definition.Members.Add(method);
		}

		private static void GenerateConstructors(BaseClassInfo baseClass, TypeDefinition definition)
		{
			foreach (var ctor in baseClass.GetCtors())
			{
				var constructor = new Constructor(definition.LexicalInfo);
				definition.Members.Add(constructor);
				var super = new MethodInvocationExpression(new SuperLiteralExpression());
				constructor.Body.Add(super);
				foreach (var info in ctor.GetParameters())
				{
					var reference = new SimpleTypeReference(TypeUtilities.GetFullName(info.ParameterType));
					var item = new ParameterDeclaration(info.Name, reference);
					constructor.Parameters.Add(item);
					super.Arguments.Add(new ReferenceExpression(info.Name));
				}
			}
		}
	}
}